package com.jd.controller;

import com.jd.constant.Constants;
import com.jd.entity.User;
import com.jd.model.LoginResult;
import com.jd.model.Response;
import com.jd.service.LoginService;
import com.jd.service.UserService;
import com.jd.vo.LoginParamVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextHolderStrategy;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

/**
 * @author wangshuping
 * @version 1.0
 * @date 2021/11/21 14:38
 */
@RestController
public class LoginController {


    @Autowired
    private LoginService loginService;

    @Autowired
    private UserService userService;


    @PostMapping("/login")
    @ApiOperation(value = "后台登录")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "username", value = "用户名称"),
                    @ApiImplicitParam(name = "password", value = "用户的密码"),
            }
    )
    public Response<LoginResult> login(@RequestBody LoginParamVo loginParamVo) {
        return Response.ok(loginService.login(loginParamVo.getUsername(), loginParamVo.getPassword()));
    }


    @GetMapping("/getUserInfo")
    @ApiOperation(value = "获取用户信息")
    public Response<User> userInfo() {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        return Response.ok(userService.getById(userId));
    }

    /**
     * TODO 后面做 (研究不能用logout退出的问题)
     * @return
     */
    @PostMapping("/doLogout")
    @ApiOperation(value = "退出登录")
    public Response<User> logout() {
        return Response.ok();
    }
}
